<template>
  <div>
    <top-bar></top-bar>
    <left-bar :datas='data'></left-bar>
    <div class="main">
      <div class="main-mains">
        <div class="main-content">
          <div class="main-mains-top">
            问卷列表
          </div>
          <div class="screen clear">
            <div class="fl" @click="addQuestionnaire()">新增</div>
            <div class="fl" @click="screen()">筛选</div>
          </div>
          <div class="publicTable toLongTable">
            <table class="head_table">
              <tr>
                <td width="5%">问卷名称</td>
                <td width='5%'>对应任务单元</td>
                <td width='12%'>所属项目</td>
                <td width='7%'>状态</td>
                <td width='8%'>开始时间</td>
                <td width='8%'>截止时间</td>
                <td width='12%'>创建时间</td>
                <td width='12%'>操作</td>
              </tr>
            </table>
            <el-scrollbar>
              <table class="content_table">
                <tr v-for="(item,index) in listData" :key="index" :class="{active:index%2==1}">
                  <td width="5%">{{item.title}}</td>
                  <td width='5%'>{{item.project_item}}</td>
                  <td width='12%'>{{item.project}}</td>
                  <td width='7%'>{{item.active | activeType}}</td>
                  <td width='8%'>{{item.starttime}}</td>
                  <td width='8%'>{{item.expires}}</td>
                  <td width='12%'>{{item.create_time | times}}</td>
                  <td width='12%'>
                    <span class="active" @click="editQue(item.id)">编辑</span>&nbsp;
                    <span class="active" @click="getDetails(item.id)">查看</span>&nbsp;
                    <span class="active" @click="removeData(item.id)">删除</span>
                  </td>
                </tr>
              </table>
            </el-scrollbar>
          </div>
          <div class="nodataTip" v-if="showNodataTip">
            <i class="iconfont icon-zanwuneirong"></i>
            <h5>暂无内容</h5>
          </div>
          <div class="orderBotom">
            <div class="orderBotomRight">
              <el-pagination @current-change="handleCurrentChange" :page-size="15" layout="total, prev, pager, next" :total='totalnum'>
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
      <div class="system-menban" v-if="search">
      </div>
      <div class="systemPopup" v-if="search">
        <div class="topTitle">
          <span>新增项目任务</span>
          <i class="el-icon-close system-icon" @click="hideSystemPopup"></i>
        </div>
        <div class="systemPopup-con">
          <div class="systemPopup-cons clear">
            <div class="fl">时间</div>
            <div class="fl sel">
              <el-date-picker v-model="time" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </div>
          </div>
          <div class="systemPopup-cons clear">
            <div class="fl">关键词搜索</div>
            <div class="fl sel">
              <el-input placeholder="请输入问卷名称/所属项目名称/任务单元名称" v-model.trim="keyword" clearable>
              </el-input>
            </div>
          </div>
          <div class="systemPopup-btn clear">
            <div class="fl" @click="searchs()">确认</div>
            <div class="fl" @click="hideSystemPopup">取消</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import leftBar from "@/components/leftBar";
import topBar from "@/components/topBar";
import questionnaire from "@/api/questionnaire";
export default {
  components: {
    leftBar,
    topBar
  },
  data() {
    return {
      data: "questionnaire",
      pages: 1,
      listData: [],
      totalnum: 0,
      showNodataTip: false,
      search: false,
      time: "",
      keyword: ""
    };
  },
  methods: {
    editQue(id) {
      this.$router.push("/editquestion?id=" + id);
    },
    removeData(id) {
      this.$confirm("此操作将删除该问卷, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const options = {
            id: id
          };
          questionnaire.deleteQuestion(options).then(res => {
            if (res.code == 1) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    addQuestionnaire() {
      this.$router.push("/addquestion");
    },
    getDetails(id) {
      this.$router.push("/questionUser?id=" + id);
    },
    screen() {
      this.search = true;
    },
    hideSystemPopup() {
      this.search = false;
    },
    handleCurrentChange(val) {
      this.pages = val;
      this.getList();
    },
    searchs() {
      this.pages = 1;
      this.getList();
    },
    getList() {
      this.showNodataTip = false;
      let options = {};
      if (this.time == null || this.time == "") {
        options = {
          page: this.pages,
          keywords: this.keyword,
          sta_date: "",
          end_date: "",
          rows: "15"
        };
      } else {
        options = {
          page: this.pages,
          keywords: this.keyword,
          sta_date: this.time[0],
          end_date: this.time[1],
          rows: "15"
        };
      }

      questionnaire.getList(options).then(res => {
        if (res.code == 1) {
          this.search = false;
          this.listData = res.data.list;
          this.totalnum = res.data.total;
          if (!res.data.total) {
            this.showNodataTip = true;
          }
        }
      });
    }
  },
  mounted() {
    this.getList();
  },
  filters: {
    activeType(val) {
      let type;
      if (val == 1) {
        type = "正常";
      } else if (val == 2) {
        type = "未开始";
      } else if (val == 3) {
        type = "已过期";
      } else if (val == 4) {
        type = "暂停";
      }
      return type;
    },
    times(value) {
      var time = new Date(value * 1000);
      var month =
        time.getMonth() + 1 > 9
          ? time.getMonth() + 1
          : "0" + (time.getMonth() + 1);
      var data = time.getDate() > 9 ? time.getDate() : "0" + time.getDate();
      var hour = time.getHours() > 9 ? time.getHours() : "0" + time.getHours();
      var minutes =
        time.getMinutes() > 9 ? time.getMinutes() : "0" + time.getMinutes();
      var second =
        time.getSeconds() > 9 ? time.getSeconds() : "0" + time.getSeconds();
      var times =
        time.getFullYear() +
        "-" +
        month +
        "-" +
        data +
        " " +
        hour +
        ":" +
        minutes +
        ":" +
        second;
      return times;
    }
  }
};
</script>

<style>
</style>
